<?php

/**
 * 订单管理
 */
namespace app\avikcm\controller;
use think\Db;
use think\facade\Request;

class Order extends Common
{
    public function initialize()
    {
        parent::initialize();
    }


    /* 订单 */
    public function lists(){
        if(Request::isAjax()){
            $key = input('post.key');
            $order_status = input('post.order_status');
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('pageSize');
            $where = [];
            $where[] = ['a.order_type', '=', 1];
            if ($key) {
                $where[] = ['a.order_no|b.goods_name|c.realname', 'like', "%" . $key . "%"];
            }
          
            if($order_status){
                $where[] = ['a.order_status','=',$order_status];
            }
            $paginate = ['list_rows'=>$pageSize,'page'=>$page];
            $list = db('order_info')->alias('a')
                ->leftJoin('order_goods b','a.id = b.order_id')
                ->leftJoin('users c','c.id = a.user_id')
                ->field('a.*,c.realname,b.goods_name,b.goods_num')
                ->where($where)
                ->order('a.id', 'desc')
                ->paginate($paginate)->toArray();
            return ['code'=>0,'msg'=>'成功','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        return $this->fetch();
    }

    public function look(){
        $id = input('id');
        $order_status = [1=>'待支付',2=>'待发货',3=>'已发货',4=>'已完成',5=>'已取消'];
        $pay_method = [1=>'微信支付',2=>'余额支付'];
        $info = db('order_info')->alias('a')
            ->field('a.*,b.nickname')
            ->join('users b','b.id = a.user_id')
            ->where('a.id',$id)
            ->find();
        $info['order_status_name'] = $order_status[$info['order_status']];
        $info['pay_method_name'] = $pay_method[$info['pay_method']];

        $goods_list = db('order_goods')->field('goods_id,sku_id,goods_name,goods_pic,goods_num,sku_name,sku_price')->where('order_id', $id)->select(); 
        $expressCompany = db('express_company')->where('id',$info['express_id'])->find();
        $info['express_name'] = $expressCompany['name'];
        $this->assign('info',$info);
        $this->assign('goods_list', $goods_list);
        $this->assign('title','订单详情');
        return $this->fetch('look');
    }

    public function cancelOrder(){
        $id = input('id');
        $res = db('order_info')->where('id',$id)->update(['order_status'=>5]);
        if($res){
            return ['code'=>1,'msg'=>'操作成功！'];
        }else{
            return ['code'=>0,'msg'=>'操作失败！'];
        }
    }

    public function getExpressList(){
        $res = db('express_company')->select();
        return $res;
    }

    public function sendGoods(){
        $id = input('id');
        $company = input('company');
        $express = input('express');

        $order = db('order_info')->where('id',$id)->find();
        $data = [
            'express_id'=>$company,
            'express_num'=>$express,
            'order_status'=>3,
            'send_time' => date('Y-m-d H:i:s')
        ];
        $res = db('order_info')->where('id',$id)->update($data);

        if($res){
            return ['code'=>1,'msg'=>'操作成功！'];
        }else{
            return ['code'=>0,'msg'=>'操作失败！'];
        }
    }

    public function refundGoods(){
        $id = input('id');
        $company = input('company');
        $express = input('express');
        $data = [
            'express_id'=>$company,
            'express_num'=>$express,
            'order_status'=>7,
            'send_time' => date('Y-m-d H:i:s')
        ];
        $res = db('order_info')->where('id',$id)->update($data);
        if($res){
            return ['code'=>1,'msg'=>'操作成功！'];
        }else{
            return ['code'=>0,'msg'=>'操作失败！'];
        }
    }

    /* 课程订单 */
    public function courseList()
    {
        if (Request::isAjax()) {
           
            $key = input('post.key');
            $order_status = input('post.order_status');
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('pageSize');
            $where = [];
            $where[] = ['a.order_type', '=', 2];
            if ($key) {
                $where[] = ['a.order_no|b.title|c.realname', 'like', "%" . $key . "%"];
            }
           
            // if ($order_status) {
            //     $where[] = ['a.order_status', '=', $order_status];
            // }
            $paginate = ['list_rows' => $pageSize, 'page' => $page];
            $list = db('order_info')->alias('a')
                ->leftJoin('order_course b', 'a.id = b.order_id')
                ->leftJoin('users c', 'c.id = a.user_id')
                // ->join('express_company d','a.express_id = d.id')
                ->field('a.*,c.realname,b.title')
                ->where($where)
                ->order('a.id', 'desc')
                ->paginate($paginate)->toArray();
            return ['code' => 0, 'msg' => '成功', 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }
        return $this->fetch();
    }

    public function courseDetail()
    {
        $id = input('id');
        $order_status = [1 => '待支付', 2 => '待发货', 3 => '已发货', 4 => '已完成', 5 => '已取消'];
        $pay_method = [1 => '微信支付', 2 => '余额支付'];
        $info = db('order_info')->alias('a')
            ->field('a.*,b.nickname')
            ->join('users b', 'b.id = a.user_id')
            ->where('a.id', $id)
            ->find();
        $info['order_status_name'] = $order_status[$info['order_status']];
        $info['pay_method_name'] = $pay_method[$info['pay_method']];

        $course_list = db('order_course')->where('order_id', $id)->select();
        $expressCompany = db('express_company')->where('id', $info['express_id'])->find();
        $info['express_name'] = $expressCompany['name'];
        $this->assign('info', $info);
        $this->assign('course_list', $course_list);
        $this->assign('title', '订单详情');
        return $this->fetch();
    }

    

}